/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.DAO;

import desu.db.HibernateUtil;
import desu.nya.Genre;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Fess
 */
public class GenreIMPL implements GenreDAO{
  
    @Override
    public void addGenre(Genre genre) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(genre);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при вставке жанра");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public Genre getGenreByID(int id) throws SQLException {
        Session session=null;
        Genre genre=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            genre=(Genre)session.load(Genre.class, id);
        }
        catch(Exception ex){
            System.err.println("Ошибка при поиске жанра");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return genre;
    }

    @Override
    public void updateGenre(int genre_id, Genre genre) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(genre);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при обновлении жанра");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Genre> getAllGenre() throws SQLException {
        Session session=null;
        List<Genre> list=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            list=(List<Genre>)session.createCriteria(Genre.class).addOrder(Order.asc("genreName")) .list();
        }
        catch(Exception ex){
            System.err.println("Ошибка при получении списка жанров");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return list;
    }

    @Override
    public void deleteGenre(Genre genre) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(genre);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при удалении жанра");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public List<String> getIdListByNameList(List<String> genreIdList) throws SQLException {
        Session session=null;
        List<String> list=null;
        if(genreIdList==null)
            return null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
//            list=(List<Genre>)session.createCriteria(Genre.class).list();
//            Criteria cr=session.createCriteria(Genre.class);
//            for(String str:genreIdList)
//                cr.add(Expression.eq("genreName", str));
            String hql="Select g.id from Genre g where ";
            int fl=0;
            for(String str:genreIdList){
                if(fl!=0)hql+=" OR ";
                hql+="g.genreName=:gn"+fl;
                fl++;
            }
            Query q=session.createQuery(hql);
            fl=0;
            for(String str:genreIdList){
                q.setString("gn"+fl, str);
                fl++;
            }
            list=q.list();
        }
        catch(Exception ex){
            System.err.println("Ошибка при получении списка жанров");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return list;
    }
    
}
